﻿using BZ.Logger;
using NDK.Module.Model;
using NDK.Motion.BLL;
using NDK.Motion.Monitor;
using System;
using System.Collections.Concurrent;
using System.ComponentModel;
using System.Diagnostics;
using System.Threading;

namespace NDK.Module.StationLogic
{
    public class Station_转盘八穴 : StationLogic
    {
        #region 通用
        public override event PropertyChangedEventHandler PropertyChanged;

        public static Station_转盘八穴 Instance = new Station_转盘八穴();
        public Stopwatch SunWay08 = new Stopwatch();
        public string StationName = "Station08转盘8工位";


        public ConcurrentQueue<int> TurntableId;
        //步骤字段、属性
        private StationStep _Step = StationStep.判断是否工作;
        [Monitor(Description = "当前步骤")]
        public StationStep Step
        {
            get
            {
                return _Step;
            }
            set
            {
                if(_Step != value)
                {
                    if (!Common.BP_是否屏蔽计时)
                    {
                        WriteStationCTLog("StationCTLog", $"{_Step}用时为：{swStepTime.ElapsedMilliseconds} ms");
                        swStepTime.Restart();
                    }
                    _Step = value;
                    PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(value.ToString()));
                }
            }
        }
        #endregion

        /// <summary>
        /// 工站初始化
        /// </summary>
        public override void Init ()
        {
            base.Init();
            StationID = 8;
            CurHoleId = 2;
            SignalComplete = false;
            logBaseInfo = "转盘八穴流程";
            TurntableId = new ConcurrentQueue<int>();
            Step = StationStep.判断是否工作;
        }

        public override void ActionProcess ()
        {
            try
            {
                switch(Step)
                {
                    case StationStep.判断是否工作:
                        if(!Common.AllStationCanWork)
                            break;
                        if(!Common.DicStationWorkStatus[StationID])
                        {
                            Step = Common.DicHoleCanWork[CurHoleId] ? StationStep.允许下料工位取料 : StationStep.工位流程完成;
                            SunWay08.Restart();
                        }
                        break;

                    case StationStep.允许下料工位取料:
                        SignalComplete = true;
                        Step = StationStep.等待下料工位取料到位;
                        SunWay08.Restart();
                        break;

                    case StationStep.等待下料工位取料到位:
                        if(Station_转盘下料.Instance.SignalRequest)
                        {
                            Step = StationStep.关闭真空吸;
                        }
                        break;

                    case StationStep.关闭真空吸:
                        Machine.VacuunArray_转盘真空吸[CurHoleId - 1].OFF();
                        Machine.VacuunArray_转盘真空破[CurHoleId - 1].ON();
                        Thread.Sleep(50);
                        Machine.VacuunArray_转盘真空破[CurHoleId - 1].OFF();
                        SignalComplete = false;
                        TurntableId.Enqueue(CurHoleId);
                        Step = StationStep.等待下料工位离开;
                        break;

                    case StationStep.等待下料工位离开:
                        if (!Station_转盘下料.Instance.SignalRequest)
                        {
                            if (Common.GRR循环模式)
                            {
                                Step = StationStep.GRR等待下工位完成;
                            }
                            else
                            {
                                Common.ChangeHoleStatus(CurHoleId, false);
                                Step = StationStep.工位流程完成;
                            }
                        }
                        break;

                    case StationStep.工位流程完成:
                        CurHoleId++;
                        if(CurHoleId > 8)
                        {
                            CurHoleId = 1;
                        }
                        Common.ChangeTurnTableStatus(StationID, true);
                        Step = StationStep.判断是否工作;
                        break;
                    case StationStep.GRR等待下工位完成:
                        if(Station_转盘下料.Instance.GRRSignalComplete)
                        {
                            Machine.VacuunArray_转盘真空吸[CurHoleId - 1].ON();
                            GRRSignalRequest = true;
                            Step = StationStep.GRR等待下工位完成1;
                        }
                        break;
                    case StationStep.GRR等待下工位完成1:
                        if(!Station_转盘下料.Instance.GRRSignalComplete)
                        {
                            GRRSignalRequest = false;
                            Step = StationStep.工位流程完成;
                        }
                        break;
                }
            }
            catch(Exception ex)
            {
                UILog_Error($"[{logBaseInfo}] {ex.Message}");
            }
        }

        public override void EmptyActionProcess ()
        {
            ActionProcess();
        }

        public override void GRRActionProcess ()
        {
            ActionProcess();
        }

        public enum StationStep
        {
            判断是否工作,
            允许下料工位取料,
            等待下料工位取料到位,
            关闭真空吸,
            允许下料工位离开,
            等待下料工位离开,
            工位流程完成,
            GRR等待下工位完成,
            GRR等待下工位完成1,
        }
    }

}
